﻿using System;
using System.Data;
using CRM.Core.Entities;
using CRM.Core.DataInterfaces;
using System.Collections.Generic;
using CRM.Data.Common;

namespace CRM.Data
{
    public class CustomerRepository : BaseRepository<CustomerEntity>, ICustomerRepository
    {
        /// <summary>
        /// Calling Base get method & returning the output with the mentioned entity type [Here CustomerEntity].
        /// </summary>
        public IList<CustomerEntity> Get(params object[] parameters)
        {
                try
                {
                    return base.Get(StoredProcedureName, parameters);
                }
                catch (Exception)
                {
                    //passing the hardcoded values if database is not working.
                    return new List<CustomerEntity>  
                                                            { new CustomerEntity {CustomerId=1,CustomerName = "Laxman"},
                                                               new CustomerEntity {CustomerId=2,CustomerName = "Vivek"},
                                                               new CustomerEntity {CustomerId=3,CustomerName = "Vikas"},
                                                               new CustomerEntity {CustomerId=4,CustomerName = "Swapnil"},
                                                               new CustomerEntity {CustomerId=5,CustomerName = "Anthony"}
                                                             };    
                }
        }
        



        
        /// <summary>
        /// Below method i had wrote for just demonstration purpose
        /// if in future i required to write such kind of method in customer or such classses, We can easily write that.
        /// </summary>
        public IList<CustomerEntity> GetFilteredCustomers(params object[] parmeters)
        {
            //Currently i'm giving call to same procedure but you can assume that this call can be to different proc's also.
            IDataReader dataReader = db.ExecuteReader(StoredProcedureName, parmeters);
            List<CustomerEntity> list = ListConversionExtension.ConvertToList<CustomerEntity>(dataReader);
            return list;
        }

        private const string StoredProcedureName = "TEST_CRM.GET_SITE_NAME";
    }
}